﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Kamikaze
{
    class level1 : SceneBase
    {
        Objeto2D paused;

        Personagem personagem;

        RolagemCena rolagemCena;

        List<Obstaculos> listaObstaculos;

        int timer,tempo,tempoMax;
        Random random;

        KeyboardState teclado,oldTeclado;

        bool pause;

        private Song song;
        //SoundEffect effect;
        
       



        public override void start()
        {
            pause = false;

            song = (Game1.Self.Content.Load<Song>(@"Sounds/jungle-run"));

            MediaPlayer.Play(song);
            MediaPlayer.IsRepeating = true;



            personagem = new Personagem(Game1.Self.Content.Load<Texture2D>(@"Imagens/skate"), 50, 66);
            personagem.posicao = new Vector2(300, 300);
            //toco = new Objeto2D(Game1.Self.Content.Load<Texture2D>("toco"));
            //toco.posicao = new Vector2(500, 330);

            paused = new Objeto2D(Game1.Self.Content.Load<Texture2D>(@"Imagens/pause"));
            paused.posicao = new Vector2(320, 200);


            rolagemCena = new RolagemCena(
                Game1.Self.Content.Load<Texture2D>(@"Imagens/cenario1"),
                Game1.Self.Content.Load<Texture2D>(@"Imagens/cenario1"),
                Game1.Self.Content.Load<Texture2D>(@"Imagens/fundo"),
                Game1.Self.Content.Load<Texture2D>(@"Imagens/fundo"), 
                0, 0);

            listaObstaculos = new List<Obstaculos>();

            timer = 0;
            random = new Random();

            tempo = 0;
            tempoMax = 3600;

        }






        public override void update(GameTime gameTime)
        {
        teclado = Keyboard.GetState();

          
            
            if (!pause)
            {
                paused.visible = false;
                rolagemCena.Update(gameTime);

                personagem.Update(gameTime);

                if (timer >= 200)
                {
                    listaObstaculos.Add(new Obstaculos(Game1.Self.Content.Load<Texture2D>(@"Imagens/pedra"),
                        new Vector2(Game1.Self.Window.ClientBounds.Width + 50,
                        random.Next((int)(Game1.Self.Window.ClientBounds.Height * 0.72f), Game1.Self.Window.ClientBounds.Height - 30))));

                    timer = 0;

                }
                else
                {
                    timer++;
                }





                if (listaObstaculos != null)
                {
                    for (int x = 0; x < listaObstaculos.Count; x++)
                    {
                        listaObstaculos[x].Update();

                        if (listaObstaculos[x].hitTestObject(personagem))
                        {

                            personagem.visible = false;
                            SceneManager.setScene(new gameOver());
                        }

                    }
                }

              
                                             
            }

            if ((teclado.IsKeyDown(Keys.Escape)))
            {
                SceneManager.setScene(new Menu());
            }


            if ((teclado.IsKeyDown(Keys.Enter)) && (!oldTeclado.IsKeyDown(Keys.Enter)))
            {
                Console.WriteLine("pause");
                pause = !pause;
                paused.visible = true;

            }



            if (tempo > tempoMax)
            {
                SceneManager.setScene(new Menu());
            }
            else
            {
                tempo++;
            }

   


            oldTeclado = teclado;

        }
        public override void draw(SpriteBatch spriteBatch)
        {
            
            rolagemCena.Draw(spriteBatch);
          
            personagem.Draw(spriteBatch);
            paused.Draw(spriteBatch);
            //pedra.Draw(spriteBatch);

            if (listaObstaculos != null)
            {
                for (int x = 0; x < listaObstaculos.Count; x++)
                {
                    listaObstaculos[x].Draw(spriteBatch);  
    
                }
            }
            
           
        }

        public override void terminate()
        {
            MediaPlayer.Stop();
            tempo = 0;
        }

    }

}


